<?php
    class Produit_m extends Model{
        function Produit_m(){
            parent::Model();
            $this->load->database();
        }
        function get_nouveaux_produits($per_page,$offset){
            $this->db->select('zoo_produit.*,zoo_categories.nomfr as catnom');
            $this->db->from('zoo_produit');
            $this->db->join('zoo_categories', 'zoo_categories.id = zoo_produit.catid','left');
            $this->db->order_by('zoo_produit.id','DESC');
            $this->db->limit($per_page,$offset);
            $query=$this->db->get();
            return $query->result_array();
        }
         function get_soldes_produits($per_page,$offset){
            $this->db->select('zoo_produit.*,zoo_categories.nomfr as catnom');
            $this->db->from('zoo_produit');
            $this->db->join('zoo_categories', 'zoo_categories.id = zoo_produit.catid','left');
           $this->db->where('zoo_produit.promo > ','0');
            $this->db->order_by('zoo_produit.id','random');
           $this->db->limit($per_page,$offset);
            $query=$this->db->get();
            return $query->result_array();
        }

         function get_coeurs_produits($per_page,$offset){
            $this->db->select('zoo_produit.*,zoo_categories.nomfr as catnom');
            $this->db->from('zoo_produit');
            $this->db->join('zoo_categories', 'zoo_categories.id = zoo_produit.catid','left');
           $this->db->where('zoo_produit.coeur','1');
            $this->db->order_by('zoo_produit.id','random');
           $this->db->limit($per_page,$offset);
            $query=$this->db->get();
            return $query->result_array();
        }
         function get_all_categories(){
      	 $query = $this->db->get_where('zoo_categories', array('parent'=>'0'));
     	 return $query->result_array();
      }

       function get_all_id_categories_child($catid){
       	$list=false;
       	if ($catid!=0){

       		$list['id']="(";
       		$query = $this->db->get_where('zoo_categories', array('parent'=>$catid));
              if ($query->num_rows()>0)
	               {
               	    foreach($query->result_array() as $aRow):
						$list['id'].=$aRow['id'].',';
                            endforeach;
                $list['id']= substr($list['id'],0,strlen($list['id'])-1).')';
                $list['array']= $query->result_array();
               	return $list;
           	}
			else{
				$array=array('id' => $catid, 'parent >' => 0);
			$this->db->select();
			 $this->db->from('zoo_categories');
     	  $this->db->where($array);
     	  $query=$this->db->get();
				 if ($query->num_rows()>0)
	               {
               	    foreach($query->result_array() as $aRow):
						$list['id'].=$aRow['id'].')';
                            endforeach;
                    $list['array']= $query->result_array();
               	return $list;
           	 } else return false;
			}
		} else { return $list=false;}
      }
      function get_img_categories($catid){
  		$query = $this->db->get_where('zoo_categories', array('id'=>$catid));
  		 return $query->row_array();
  	}

        function count_product_categories($list){
        
      	if($list!=false){
      		$this->db->select();
     	  $this->db->from('zoo_produit');
     	 	   $this->db->where('catid in '.$list);
     	  $query=$this->db->get();
     	    return $query->num_rows();
       }else return false;
  	}


        function get_product_categories($list,$per_page,$offset){
        
      	if($list!=false){
      		$this->db->select();
     	  $this->db->from('zoo_produit');
     	   $this->db->where('catid in '.$list);
     	  $this->db->limit($per_page,$offset);
     	  $query=$this->db->get();
     	    return $query->result_array();
       }else return false;
  	}

          function get_product($per_page,$offset){

      	 $this->db->select();
     	  $this->db->from('zoo_produit');
     	     	  $this->db->limit($per_page,$offset);
     	  $query=$this->db->get();
     	    return $query->result_array();
  	}
        function get_detail_product($id){
  		 $this->db->select();
           $this->db->from('zoo_produit');
          $this->db->where('id =',$id);
          $query=$this->db->get();
            return $query->row_array();
  	}

        function get_all_prix(){
            $this->db->select();
           $this->db->from('zoo_prix');
           $this->db->order_by('id','ASC');
           $query=$this->db->get();
            return $query->result_array();
        }
        function get_product_recherche($mot,$prix,$cat,$per_page,$offset){
          $min=0;
          $max=0;
          $listcat="";
            if($prix!="_") {
          $query = $this->db->get_where('zoo_prix', array('id'=>$prix));
          $resultat=$query->row();
          $min=$resultat->min;
          $max=$resultat->max;
          }
           if($cat!="_") {
          $query = $this->db->get_where('zoo_categories', array('parent'=>$cat));
           if ($query->num_rows()>0)
	               { 
               	    foreach($query->result_array() as $aRow):
			$listcat.=$aRow['id'].',';
                    endforeach;
             $listcat= '('.substr($listcat,0,strlen($listcat)-1).')';
           	 } else $listcat="()";

          }

            $this->db->select();
     	  $this->db->from('zoo_produit');
          if($mot!="_") $this->db->like('nomfr', $mot);
          if($cat!="_") $this->db->where('catid in '.$listcat);
          if($prix!="_") {
              $this->db->where('(prix-promo) <= ',$max);
              $this->db->where('(prix-promo) >= ',$min);
              }
     	  $this->db->limit($per_page,$offset);
     	  $query=$this->db->get();
     	    return $query->result_array();
        }

         function count_product_recherche($mot,$prix,$cat){
          $min=0;
          $max=0;
          $listcat="";
            if($prix!="_") {
          $query = $this->db->get_where('zoo_prix', array('id'=>$prix));
          $resultat=$query->row();
          $min=$resultat->min;
          $max=$resultat->max;
          }
           if($cat!="_") {
          $query = $this->db->get_where('zoo_categories', array('parent'=>$cat));
           if ($query->num_rows()>0)
	               {
               	    foreach($query->result_array() as $aRow):
			$listcat.=$aRow['id'].',';
                    endforeach;
             $listcat= '('.substr($listcat,0,strlen($listcat)-1).')';
           	 } else $listcat="()";

          }

            $this->db->select();
     	  $this->db->from('zoo_produit');
          if($mot!="_") $this->db->like('nomfr', $mot);
          if($cat!="_") $this->db->where('catid in '.$listcat);
          if($prix!="_") {
              $this->db->where('prix <= ', $max);
              $this->db->where('prix >= ', $min);
              }
     	 
     	  $query=$this->db->get();
     	    return $query->num_rows();
        }
        function add_echantillon($id_user,$id_produit){
            $data=array(
            'product_id' => $id_produit,
            'user_id' => $id_user
            );
           return $this->db->insert('zoo_test',$data);

        }
        function check_echantillon($id_user,$id_produit){

          $query = $this->db->get_where('zoo_test', array('user_id'=>$id_user,'product_id'=>$id_produit));
          return $query;
        }

        function get_all_echantillons($id_user,$perpage,$offset){
             $this->db->select('zoo_test.*,zoo_produit.nomfr as nomproduit');
     	  $this->db->from('zoo_test');
          $this->db->join('zoo_produit','zoo_produit.id=zoo_test.product_id');
          $this->db->where('user_id',$id_user);
           $this->db->order_by('id','DESC');
     	  $this->db->limit($perpage,$offset);
     	  $query=$this->db->get();
          return $query->result_array();
        }
        function count_all_echantillons($id_user){
             $this->db->select();
     	  $this->db->from('zoo_test');
          $this->db->where('user_id',$id_user);
     	  $query=$this->db->get();
          return $query->num_rows();
        }
        
        function get_poids($panier){
           $poids=0;
            foreach ($panier as $aRow):
                $poids=$poids+($aRow['poids']*$aRow['qty']);
            endforeach;
            return $poids;
        }
        function get_livraison($poids){
           
             $this->db->select('zoo_tarif.*,zoo_transporteur.nom as nom,zoo_transporteur.duree as duree');
           $this->db->from('zoo_tarif');
           $this->db->join('zoo_transporteur','zoo_transporteur.id=zoo_tarif.id_transporteur');
          $this->db->where('zoo_tarif.min <=',$poids);
           $this->db->where('zoo_tarif.max >',$poids);
          $this->db->order_by('zoo_tarif.tarif','ASC');
          $query=$this->db->get();
           return $query->result_array();

        }
        function check_code_promo($promo){
          $query= $this->db->get_where('zoo_promotion',array('code'=>$promo));
           return $query->num_rows() >0;
        }
       function get_pct_promo($promo){
             $query= $this->db->get_where('zoo_promotion',array('code'=>$promo));
           return $query->row_array();
        }
        function get_client($id){
           $query= $this->db->get_where('lehu_profiles',array('user_id'=>$id));
           return $query->row_array();
        }
         function get_user($id){
           $query= $this->db->get_where('lehu_users',array('id'=>$id));
           return $query->row_array();
        }
        function get_all_commandes($id_user,$perpage,$offset){
            $this->db->select();
     	  $this->db->from('zoo_commande');
 			$this->db->where('idclient',$id_user);
     	  $this->db->limit($perpage,$offset);
     	  $query=$this->db->get();
     	    return $query->result_array();
        }
        function count_commandes($id_user){
            $this->db->select();
     	  $this->db->from('zoo_commande');
             $this->db->where('idclient',$id_user);
     	  $query=$this->db->get();
     	    return $query->num_rows();
        }
    }
?>